package com.aaa.hospital.ManagementEnd.controller;
import com.aaa.hospital.ManagementEnd.pojo.Docuser;
import com.aaa.hospital.ManagementEnd.pojo.Nurse;
import com.aaa.hospital.ManagementEnd.service.Impl.NurseImpl;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Nurse")
public class NurseCon {
    @Autowired
    private NurseImpl nurseImpl;

    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    // 将java对象转为json格式的数据
    @ResponseBody
    //查询医生信息
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<Nurse> data = nurseImpl.queryAll();
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> insert(Nurse nurse) {
        int result = nurseImpl.insert(nurse);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //删除
    @RequestMapping(value = "/{nurid}", method = RequestMethod.GET)
    @ResponseBody
    //@PathVariable注解绑定它传过来的值到方法的参数上。
    private Map<String, Object> delete(@PathVariable("nurid") Integer nurid) {
        int result = nurseImpl.delete(nurid);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //修改
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> update(Nurse nurse) {
        int result = nurseImpl.update(nurse);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //搜索框查询
    @RequestMapping(name="根据护士名模糊查询和根据护士精确查询护士信息",value = "/selectN_id_Name",method = RequestMethod.GET)
    @ResponseBody
    public String selectName(@Param("nurid") Integer nurid, @Param("name") String name){
        JSONObject json = new JSONObject();
        List<Nurse> data = nurseImpl.selectN_id_Name(nurid,name);
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }

}
